package middle;

public class Solution_198 {

    public int rob(int[] nums) {
        int[] array = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            if (i < 2) {
                array[i] = nums[i];
            } else if (i == 2) {
                array[i] = nums[i] + nums[i - 2];
            } else {
                array[i] = nums[i] + Math.max(array[i - 2], array[i - 3]);
            }
        }
        int max = 0;
        for (int num : array) {
            max = Math.max(max, num);
        }
        return max;
    }
    /*public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        } else if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        int[] array = new int[nums.length];
        array[0] = nums[0];
        array[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < nums.length; i++) {
            array[i] = Math.max(array[i - 2] + nums[i], array[i - 1]);
        }
        return array[array.length - 1];
    }*/

    public static void main(String[] args) {
        Solution_198 model = new Solution_198();
        System.out.println(model.rob(new int[]{1, 2, 3, 1}));
        System.out.println(model.rob(new int[]{2, 7, 9, 3, 1}));
        System.out.println(model.rob(new int[]{2, 1, 1, 2}));
    }
}
